package com.avicsafety.lib.tools;

import android.content.ContentValues;
import android.provider.MediaStore;
import android.widget.Toast;

import com.avicsafety.lib.interfaces.OnNetworkAccessToListListener;
import com.avicsafety.lib.interfaces.OnNetworkAccessToMessageListener;

import org.xutils.common.Callback;
import org.xutils.common.util.LogUtil;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/5/31.
 */

public class ImageUtil {

    public static void DownLoadImagesByUrl(String filesDir, final String fileName, String url) {
        //存到本地的绝对路径
        final String filePath = filesDir + "/" + fileName;
        File file = new File(filesDir + "/");
        //如果不存在
        if (!file.exists()) {
            //创建
            file.mkdirs();
        }

        RequestParams entity = new RequestParams(url);
        entity.setSaveFilePath(filePath);
        x.http().get(entity, new Callback.CommonCallback<File>() {
            @Override
            public void onSuccess(File result) {
//                filesPath.add(result.getAbsolutePath());
                LogUtil.e("onSuccess：" + result.getAbsolutePath());
                ContentValues values = new ContentValues();
                values.put(MediaStore.Images.Media.DATA, filePath);
                values.put(MediaStore.Images.Media.TITLE, fileName);
                x.app().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                L.e("onError ");
                Toast.makeText(x.app(), "网络错误，图片下载失败", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {
                L.e("onCancelled ");
            }

            @Override
            public void onFinished() {
                L.e("onFinished ");
            }
        });
    }

    public static void DownLoadImagesByUrl(String filesDir, final String fileName, String url, final OnNetworkAccessToMessageListener listener) {
        //存到本地的绝对路径
        final String filePath = filesDir + "/" + fileName;
        File file = new File(filesDir + "/");
        //如果不存在
        if (!file.exists()) {
            //创建
            file.mkdirs();
        }

        RequestParams entity = new RequestParams(url);
        entity.setSaveFilePath(filePath);
        x.http().get(entity, new Callback.CommonCallback<File>() {
            @Override
            public void onSuccess(File result) {
//                filesPath.add(result.getAbsolutePath());
                LogUtil.e("onSuccess：" + result.getAbsolutePath());
                ContentValues values = new ContentValues();
                values.put(MediaStore.Images.Media.DATA, filePath);
                values.put(MediaStore.Images.Media.TITLE, fileName);
                x.app().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
                listener.onSuccess(filePath);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                L.e("onError ");
                //Toast.makeText(x.app(), "网络错误，图片下载失败", Toast.LENGTH_SHORT).show();
                listener.onFail("图片下载失败");
            }

            @Override
            public void onCancelled(CancelledException cex) {
                L.e("onCancelled ");
            }

            @Override
            public void onFinished() {
                L.e("onFinished ");
            }
        });
    }



    public static void DownLoadImagesByUrls(final List<String> url_list, final String filesDir, final OnNetworkAccessToListListener<String> listener) {
        List<String> urls = new ArrayList<>();
        List<String> list_path = new ArrayList<>();
        urls.addAll(url_list);
        downImagesByUrls(filesDir,listener,0,list_path,urls);
    }

    public static void DownLoadImagesByUrls(String [] url,final String filesDir, final OnNetworkAccessToListListener listener) {
        List<String> urls = new ArrayList<>();
        List<String> list_path = new ArrayList<>();
        for (int i=0;i<url.length;i++) {
            urls.add(url[i]);
        }
        downImagesByUrls(filesDir,listener,0,list_path,urls);
    }


    private static void downImagesByUrls(final String filesDir, final OnNetworkAccessToListListener listener, final int index, final List<String> list_path, final List<String> urls) {
        if (index < urls.size()) {
            //存到本地的绝对路径
            final String fileName = new GuidCreator().toString();
            final String filePath = filesDir + "/" + fileName+index+".jpg";
            File file = new File(filesDir + "/");
            //如果不存在
            if (!file.exists()) {
                //创建
                file.mkdirs();
            }

            RequestParams entity = new RequestParams(urls.get(index));
            entity.setSaveFilePath(filePath);
            list_path.add(filePath);

            x.http().get(entity, new Callback.CommonCallback<File>() {
                @Override
                public void onSuccess(File result) {
                    LogUtil.e("onSuccess：" + result.getAbsolutePath());
                    ContentValues values = new ContentValues();
                    values.put(MediaStore.Images.Media.DATA, filePath);
                    values.put(MediaStore.Images.Media.TITLE, fileName);
                    x.app().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
                    downImagesByUrls(filesDir, listener,index+1,list_path,urls);
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                    L.e("onError ");
                    listener.onFail("图片下载失败");
                }

                @Override
                public void onCancelled(CancelledException cex) {
                    L.e("onCancelled ");
                }

                @Override
                public void onFinished() {
                    L.e("onFinished ");
                }
            });
        } else {
            listener.onSuccess(list_path);
        }
    }
}
